#include<string>
#include<vector>
using namespace std;

class Solution {
private:
    int isOps(const char& ch) {
        if (ch == '+') return 1;
        if (ch == '-') return 2;
        if (ch == '*') return 3;
        if (ch == '/') return 4;
        return 0;
    }

public:
    int calculate(string s) {
        vector<int> stack;
        int index = 0;

        while (index < s.length()) {
            if (s[index] == ' ') {
                ++index;
                continue;
            }
            int ops = isOps(s[index]);
            if (ops)
                ++index;
            while (index < s.length()) {
                if (s[index] == ' ')
                    ++index;
                else
                    break;
            }
            int prev = index;
            while (index < s.length()) {
                if (isOps(s[index]))
                    break;
                ++index;
            }
            int value = atoi(s.substr(prev, index - prev).c_str());
            switch (ops) {
            case 2:
                value *= -1;
                break;
            case 3:
                value *= stack.back();
                stack.pop_back();
                break;
            case 4:
                value = stack.back() / value;
                stack.pop_back();
                break;
            }
            stack.push_back(value);
        }
        int res = 0;
        for (int i = 0; i < stack.size(); ++i)
            res += stack[i];
        return res;

    }
};